class HomeController < ApplicationController
  skip_before_filter :require_activation
  
  def tooltips
  	not_show_toolbar
  end
  def index
    @body = "home"
    @topics = Topic.find_recent
    @members = Person.find_recent
    if logged_in?
      @feed = current_person.feed
      @some_contacts = current_person.some_contacts
      @requested_contacts = current_person.requested_contacts
    else
      @feed = Activity.global_feed
    end

	not_show_toolbar
	session[:secondi] = ""
	
	 if logged_in? && current_person.admin?
	 	 			if params[:cat_id].nil?
				@articles=
				Article.paginate(:all, :page => params[:page],
							   :per_page => 10,
							   :conditions => '(published=1 or published=0) and category_id = 1',:order=>'created_at desc')
			else
				@articles=
				Article.paginate(:all, :page => params[:page],
							   :per_page => 10,
							   :conditions => '(published=1 or published=0) and category_id = '+params[:cat_id],:order=>'created_at desc')
			end	
	 
						   
	 else
			if params[:cat_id].nil?
				@articles=
				Article.paginate(:all, :page => params[:page],
							   :per_page => 10,
							   :conditions => 'published=1 and category_id = 1',:order=>'created_at desc')
			else
				@articles=
				Article.paginate(:all, :page => params[:page],
							   :per_page => 10,
							   :conditions => 'published=1 and category_id = '+params[:cat_id],:order=>'created_at desc')
			end	
	 end
	  			  
    respond_to do |wants|
		
		wants.html {
		  render :template=>'shared/articles'
		}
		wants.xml  {  render :template=>'shared/articles_rss.rxml', :layout => false }
    end  
  end
  
  def searchres
    @body = "home"
    @topics = Topic.find_recent
    @members = Person.find_recent
    if logged_in?
      @feed = current_person.feed
      @some_contacts = current_person.some_contacts
      @requested_contacts = current_person.requested_contacts
    else
      @feed = Activity.global_feed
    end

		not_show_toolbar

    if (params[:searcht])&&(params[:textfield2]!=nil)
     if (params[:textfield2].strip!="")
      session[:secondi] = params[:textfield2]
      @arrkeyw = (params[:textfield2]).split(' ')
      @searchcond = "("
      @arrkeyw.each do |scond|
      	@searchcond = @searchcond+" title like '%#{scond}%' or synopsis like '%#{scond}%' or body like '%#{scond}%' or"
      end
      
      scondlen = @searchcond.length     
      
      @searchcond1 = @searchcond[0,scondlen-2] + ")"  

			@articles=
			Article.paginate(:all, :page => params[:page],
						   :per_page => 10,
						   :conditions => "published=1 and #{@searchcond1}",:order=>'created_at desc')   
		 else
			session[:secondi] = ""
			@articles=
			Article.paginate(:all, :page => params[:page],
						   :per_page => 10,
						   :conditions => 'published=1',:order=>'created_at desc')			     
		 end
		else
			session[:secondi] = ""
			@articles=
			Article.paginate(:all, :page => params[:page],
						   :per_page => 10,
						   :conditions => 'published=1',:order=>'created_at desc')		 
  	end
  	    
	    respond_to do |wants|
			
			wants.html {
			  render :template=>'shared/articles'    
			}
			wants.xml  {  render :template=>'shared/articles_rss.rxml', :layout => false }
	    end 
  
  end
end

